<template>
  <!--分配课题-->
  <div class="assign-subject">
    <InnerBreadcrumb v-model="indexKey" :path="breadPath" :showBack="true" :label-args="{name: currentBreadName}" />

    <div class="edu-page-warp" v-show="indexKey === 'MAIN'">
      <condition-card @reset="reset" @search="searchList" label-width="110px">
        <el-row>
          <el-col :span="8">
            <el-form-item :label="$t('graduationDesign.graduationYear')">
              <el-date-picker class="search-item" :clearable="false" v-model="queryForm.grade" @change="handleGradeChange" value-format="yyyy" type="year" :placeholder="$t('graduationDesign.pleaseChoose')">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item :label="$t('graduationDesign.adminCollege')">
              <el-select v-model="queryForm.collegeId" filterable @change="handleCollegeChange" class="search-item" clearable>
                <el-option v-for="item in source.studentColleges" :key="item.collegeId" :label="item.collegeIdI18n" :value="item.collegeId" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item :label="$t('graduationDesign.major')">
              <el-select v-model="queryForm.subjectId" filterable class="search-item" clearable>
                <el-option v-for="item in source.subjectOptions" :key="item.subjectId" :label="item.subjectName" :value="item.subjectId" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item :label="$t('graduationDesign.instructor')">
              <el-input v-model="queryForm.teacherName" class="search-item" :placeholder="$t('graduationDesign.pleaseEnter')"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item :label="$t('graduationDesign.subjectType')">
              <el-select v-model="queryForm.projectType" filterable class="search-item" clearable>
                <el-option v-for="item in source.subjectTypeOptions" :key="item.value" :label="item.label" :value="item.value" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item :label="$t('graduationDesign.status')">
              <el-select v-model="queryForm.isAllot" filterable class="search-item" clearable>
                <el-option v-for="item in source.statusOptions" :key="item.value" :label="item.label" :value="item.value" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </condition-card>

      <el-card shadow="never">
        <div class="edu-page-title">{{$t('graduationDesign.assignSubject')}}</div>
        <div class="edu-opertion-bar">
          <div class="float-left">
            <el-input v-model="queryForm.studentName" :placeholder="$t('graduationDesign.pleaseEnterStuSearch')" style="width:300px" @keyup.enter.native="searchList" clearable>
              <i slot="suffix" class="el-icon-search" style="margin-top:10px;cursor:pointer" @click="searchList"></i>
            </el-input>
          </div>
          <div class="float-right">
            <el-button @click="onExport" v-loading.fullscreen.lock="loading" element-loading-text="Loading" element-loading-spinner="el-icon-loading" element-loading-background="rgba(255, 255, 255, .48)">{{$t('graduationDesign.export')}}</el-button>
          </div>
        </div>
        <edu-table ref="assignSubjectTable" :url="getList" @selection-change="handleSelectChange">
          <edu-table-column :label="$t('graduationDesign.graduationYear')" prop="grade" min-width="90"></edu-table-column>
          <edu-table-column :label="$t('graduationDesign.student')" prop="studentName" min-width="100"></edu-table-column>
          <edu-table-column :label="$t('graduationDesign.adminCollege')" prop="collegeIdI18n" min-width="100"></edu-table-column>
          <edu-table-column :label="$t('graduationDesign.major')" prop="subjectIdI18n" min-width="100"></edu-table-column>
          <edu-table-column :label="$t('graduationDesign.direction')" prop="directionName" min-width="90"></edu-table-column>
          <edu-table-column :label="$t('graduationDesign.mentor')" prop="teacherName" min-width="90"></edu-table-column>
          <edu-table-column :label="$t('graduationDesign.subjectName')" prop="projectName" min-width="100"></edu-table-column>
          <edu-table-column :label="$t('graduationDesign.subjectType')" prop="projectType" min-width="100"></edu-table-column>
          <edu-table-column :label="$t('graduationDesign.assignStatus')" prop="isAllot" min-width="100">
            <template slot-scope="scope">
              <el-tag v-if="scope.row.infoId" type="success">{{$t('graduationDesign.assigned')}}</el-tag>
              <el-tag v-else type="info">{{$t('graduationDesign.unassigned')}}</el-tag>
            </template>
          </edu-table-column>
          <edu-table-column :label="$t('graduationDesign.operation')" min-width="160">
            <template slot-scope="scope">
              <el-button type="text" @click="onAssign(scope.row, 'EDIT')" v-if="scope.row.infoId">{{$t('graduationDesign.edit')}}</el-button>
              <el-button type="text" @click="onView(scope.row)" v-if="scope.row.infoId">{{$t('graduationDesign.view')}}</el-button>
              <el-button type="text" @click="onAssign(scope.row, 'ASSIGN')" v-if="!scope.row.infoId">{{$t('graduationDesign.assignSubject')}}</el-button>
            </template>
          </edu-table-column>
        </edu-table>
      </el-card>
    </div>

    <div v-if="indexKey === 'VIEW'">
      <!--分配课题查看-->
      <TopicInfo actionType="VIEW_ASSIGN" :basicInfoForm="basicInfoForm" :progressData="progressData"></TopicInfo>
    </div>
    <div v-if="indexKey === 'ASSIGN'">
      <!--分配课题 编辑-->
      <SubjectList :assignForm="subjectListForm" @assign-submit="assignSubmit" @assign-cancel="assignCancel"></SubjectList>
    </div>
  </div>
</template>
<script>
import API from "common/src/api/underGraduationDesign/reviewSubject";
import tools from 'common/src/views/underGraduationDesign/tools.js';
export default {
  name: 'assignSubject',
  data() {
    return {
      api: API,
      getList: '/api/designservice/optAllotStu/queryByPage',
      queryForm: {
        grade: '',
        collegeId: '',
        subjectId: '',
        status: '',
        teacherName: '',
        projectType: '',
        studentName: ''
      },
      indexKey: 'MAIN',
      source: {
        studentColleges: [], // 行政学院
        collegeOptions: [], // 学院
        subjectOptions: [], // 专业
        subjectTypeOptions: [{ // 课题类别
          label: "毕业论文",
          value: "毕业论文",
        }, {
          label: "毕业设计",
          value: "毕业设计",
        }],
        statusOptions: [{
          label: "未分配",
          value: false
        }, {
          label: "已分配",
          value: true
        }]
      },
      selectedData: [], // 勾选的列表数据
      breadPath: {
        label: this.$t('graduationDesign.assignSubject'),
        key: "MAIN",
        children: [
          {
            key: "VIEW",
            label: this.$t('graduationDesign.viewSubject')
          },
          {
            key: "ASSIGN",
            label: "{name}"
          }
        ]
      },
      loading: false,
      subjectListForm: {},
      basicInfoForm: {},
      progressData: [], //审核进度列表
      currentBreadName: ""
    };
  },
  mounted() {
    this.queryForm.grade = new Date().getFullYear() + "";
    this.searchList();
    this.getCollege();
    tools.initAllCollege(this.source.studentColleges);
  },
  methods: {
    searchList() {
      this.$refs.assignSubjectTable.search(this.queryForm);
    },
    reset() {
      this.queryForm = {
        grade: new Date().getFullYear() + "",
        collegeId: '',
        subjectId: '',
        status: '',
        teacherName: '',
        projectType: '',
        studentName: ''
      };
      this.source.subjectOptions = [];
      this.searchList();
    },
    handleSelectChange(val) {
      this.selectedData = val;
    },
    // 毕业年份change
    handleGradeChange(val) {
      this.queryForm.subjectId = "";
      this.source.subjectOptions = [];
      if (val && this.queryForm.collegeId) {
        this.handleCollegeChange(this.queryForm.collegeId);
      }
    },
    // 查学院
    getCollege() {
      this.api.getCollege().then(res => {
        this.source.collegeOptions = res;
      });
    },
    // 根据毕业年份学院查专业
    handleCollegeChange(val) {
      this.queryForm.subjectId = "";
      this.source.subjectOptions = [];
      if (val && this.queryForm.grade) {
        const params = {
          grade: this.queryForm.grade,
          college: val
        };
        this.api.getSubject(params).then(res => {
          this.source.subjectOptions = res.data;
        }).catch();
      }
    },
    // 查审核进度
    getReviewProgress(applyNo) {
      this.api.getProgress(applyNo).then(res => {
        this.progressData = res.data.listApproveHistory;
      }).catch();
    },
    // 查看
    onView(row) {
      const params = {
        infoId: row.infoId,
        studentId: row.studentId,
        infoType: '1'
      };
      this.api.getSubjectDetail(params).then(({ data }) => {
        this.indexKey = "VIEW";
        this.basicInfoForm = { ...(data.applicationTeacherEntity || data.applicationStudentEntity) };
        this.basicInfoForm.grade = row.grade ? row.grade + '' : '';
        this.basicInfoForm.flowNo = row.flowNo;
        this.basicInfoForm.stuCollegeI18n = data.stuCollegeI18n;
        this.basicInfoForm.stuSubjectIdI18n = data.stuSubjectIdI18n;
        this.basicInfoForm.applicantName = row.teacherName;
        this.basicInfoForm.teacherName = row.teacherName;
        this.basicInfoForm.studentName = row.studentName;
        this.progressData = [];
        if (row.flowNo) {
          this.getReviewProgress(row.flowNo);
        }
      }).catch();
    },
    // 分配课题 编辑
    onAssign(row, type) {
      this.indexKey = "ASSIGN";
      const title = type === "ASSIGN" ? this.$t('graduationDesign.assign') : this.$t('graduationDesign.edit');
      this.currentBreadName = `${title}${row.studentName}${this.$t('graduationDesign.subject')}`;
      this.subjectListForm = { ...row };
      this.subjectListForm.actionType = type;
    },
    // 分配课题提交
    assignSubmit(data, type) {
      let reqApi = null;
      if (type === "ASSIGN") {
        reqApi = this.api.assignSubmit(data);
      } else {
        reqApi = this.api.assignEdit(data);
      }
      reqApi.then(res => {
        this.indexKey = "MAIN";
        this.$message.success(this.$t('graduationDesign.submitSuccess'));
        this.searchList();
      });
    },
    assignCancel() {
      this.indexKey = "MAIN";
      this.searchList();
    },
    // 导出
    onExport() {
      const params = Object.assign(this.queryForm, { exprotList: this.selectedData });
      this.api.assignExport(params).then(res => {
        if (res.code == 200) {
          // this.delayRequest(res.data.key);
          tools.downloadFile(res.data.key, "分配课题.xls", this.api.getExportStatus, this);
        }
      });
    },
    // 查文件导出状态，等待导出
    async delayRequest(key) {
      this.loading = true;
      await new Promise(resolve => setTimeout(resolve, 3000));
      this.api.getExportStatus(key).then(res => {
        this.loading = false;
        if (res.code === 200 && res.data.status) {
          let path = 'api/designservice/common/export?path=' + res.data.path;
          window.open(path);
        } else {
          this.delayRequest(key);
        }
      }).catch(err => {
        this.loading = false;
        this.delayRequest(key);
      });
    }
  },
  components: {
    TopicInfo: () => import('../components/topicInfo'),
    SubjectList: () => import('./components/subjectList')
  }
}
</script>
<style lang="scss" scoped>
.assign-subject {
    .search-item {
        width: 100%;
    }
}
</style>
